"""
The "page" require is a variable name.
Look for the variable in the cgi form parameters.
If it is there, put the variable and value into
the profile.

Before storing the value add HTML break
tags before any newlines in the value for formatting.

Return empty string as stream output.
"""

from whiff.middleware import misc
from whiff import whiffenv
from whiff import gateway

class put(misc.utility):
    def __init__(self, page):
        self.page = page
    def __call__(self, env, start_response):
        variableName = self.param_value(self.page, env)
        variableValue = whiffenv.cgiGet(env, variableName)
        # if the variable has an interesting value, store it in the user profile with the same name
        if variableValue and variableValue.strip():
            # add HTML breaks to the variable value at newlines and trim surrounding whitespace
            variableValue = variableValue.strip().replace("\n", "\n<br>")
            resourcePath = ["profile", "item", variableName]
            gateway.putResource(env, resourcePath, variableValue)
        return self.deliver_page("", env, start_response)

__middleware__ = put
